package org.cn.cf.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConnectionPool implements java.io.Serializable {
	private static final Log LOG = LogFactory.getLog(ConnectionPool.class);
	
	private String driver = null;

	private String url = null;

	private String username = "";

	private String password = "";

	public static Vector conPool = new Vector();
	
	public static int size = 0;

	public void init(String driver, String url, String user, String password,
			int size) {
		this.driver = driver;
		this.url = url;
		this.username = user;
		this.password = password;
		this.size = size;
	}

	public void loadPool() throws Exception {
		if (driver == null)
			throw new Exception("no jdbc driver");

		if (url == null)
			throw new Exception("no url!");

		if (size < 1)
			throw new Exception("pool size too small Size=" + size);

		conPool.clear();
		try {
			Class.forName(driver).newInstance();

			for (int i = 0; i < size; i++) {				
				ConnectBean cb = new ConnectBean(i, createConnection());				
				conPool.add(cb);
				LOG.info("The ConnectBean add in pool index:"+i);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			throw new Exception(e.getMessage());
		}

	}
	

	public static boolean widenPool(ConnectBean bean){
		if(bean==null) return false;
		
		for(int i=0;i<size;i++){
			Object obj= conPool.get(i);
			if(bean.equals(obj)) return false;
		}
		conPool.add(bean);
		
		return true;
	}
	public synchronized static ConnectBean getConBean(){
		for(int i=0;i<size;i++){
			ConnectBean conBean= (ConnectBean)conPool.get(i);
			if(!conBean.isBfault())
				if(!conBean.isBUse())
					return conBean;
		}
		Connection con = null;
		try{
			String url = System.getProperty("CF_CON_URL");
			String user = System.getProperty("CF_USER");
			String password = System.getProperty("CF_PASSWORD");
			
			con = DriverManager.getConnection(url, user, password);
		}catch(Exception e){
			//e.printStackTrace();
			LOG.error(e.getMessage());
		}
		if(con==null) return null;
		
		ConnectBean cb = new ConnectBean(size,con);
		conPool.add(cb);
		LOG.info("Overtop ConnectBean add in pool index:"+size);
		size++;
		return cb;
	}
	
	public static void closePool(){
		for(int i=0;i<size;i++){
			ConnectBean conBean= (ConnectBean)conPool.get(i);
			conBean.releaseCon();
		}
		conPool.clear();
	}
	
	private Connection createConnection() throws Exception {

		Connection con = null;
		con = DriverManager.getConnection(url, username, password);
		//con = DriverManager.getConnection(url);
		return con;

	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
